<template>
	<view class="box">
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="item in addressList" :key="item.address_id" @longtap="del(item)">
				<view class="uni-media-list uni-pull-right">
					<view class="uni-media-list-logo" style="display: flex;align-items: center;justify-content: center;">
						<text @tap="toEditAddress(item)">编辑</text>
					</view>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top">{{item.true_name}}  {{item.mod_phone}}</view>
						<view class="uni-media-list-text-bottom uni-ellipsis">{{item.address}}</view>
					</view>
				</view>
			</view>
		</view>
	<view class="add">
		<button type="primary" style="margin-top:20upx" @tap="toAddAddress()">添加收货地址</button>
	</view>
	<xy-dialog ref='dialog' @confirmButton='confirmDel()'></xy-dialog>
	</view>
</template>

<script> 
	import {
		addressList,
		delAddress
	} from '../../common/api.js'
	import xyDialog from '../../components/xy-dialog.vue'
export default{
	components:{
		xyDialog
	},
	data(){
		return{
			userInfo: this.$store.state.userInfo,
			addressList: [], // 地址列表
			addressInfo: {}, //单个地址信息
		}
	},
	methods:{
		handleAddCart(){
			console.log(this.userInfo.user_token)
			let reqData = {
				user_token: this.userInfo.user_token
			}
			addressList(reqData).then( res => {
				let resData = res.data
				this.addressList = resData.result
				
			})
		},
		del(val){
			this.addressInfo = val
			this.$refs.dialog.confirm()
		},
		handleDelAddress(){
			let reqData = {
				user_token: this.userInfo.user_token,
				address_id: this.addressInfo.address_id
			}
			delAddress(reqData).then( res => {
				this.handleAddCart()
			})
		},
		confirmDel(){
			this.handleDelAddress()
		},
		toAddAddress(){
			uni.navigateTo({
				url:'./address_manage'
			})
		},
		toEditAddress(item){
			uni.navigateTo({
				url: './address_manage?addressInfo=' + JSON.stringify(item)
			})
		}
	},
	onShow() {
		this.handleAddCart()
	}
}
</script>

<style>
	@import "../../common/uni.css";
	
</style>